package controller;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/8/1 10:13
 * Version:1.0-SNAPSHOT
 * Description:
 */
@Controller
@RequestMapping("/file")
public class FileController {

    @RequestMapping("/showUpload")
    public String showUpload(){
        return "upload";
    }

    @RequestMapping("/upload")
    public void upload(String username, @RequestParam(value = "file",required = false) CommonsMultipartFile file, HttpSession session){
        System.out.println("username:"+username);
        if(!file.isEmpty()) {
            // 所有上传的资源存放到根目录upload中
            // 且每天在upload目录中创建一个以当天年月日命名的子文件夹
            // 当天上传的所有资源存放到当天创建的子目录中
            // 获取服务器路径
            String path = session.getServletContext().getRealPath("/upload/" + new SimpleDateFormat("yyyyMMdd").format(new Date()));
//        System.out.println("path:"+path);
            // 创建对应的目录
            File f = new File(path);
            f.mkdirs();
            try {
                file.transferTo(new File(path, file.getOriginalFilename()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/uploads")
    public void uploads(String username, @RequestParam(value = "file",required = false) CommonsMultipartFile[] files, HttpSession session){
        System.out.println("username:"+username);
        for(CommonsMultipartFile file : files) {
            if (!file.isEmpty()) {
                // 所有上传的资源存放到根目录upload中
                // 且每天在upload目录中创建一个以当天年月日命名的子文件夹
                // 当天上传的所有资源存放到当天创建的子目录中
                // 获取服务器路径
                String path = session.getServletContext().getRealPath("/upload/" + new SimpleDateFormat("yyyyMMdd").format(new Date()));
//        System.out.println("path:"+path);
                // 创建对应的目录
                File f = new File(path);
                f.mkdirs();
                String fileName = file.getOriginalFilename();
                fileName = fileName.substring(0,fileName.lastIndexOf("."))+"-"+new Date().getTime()+fileName.substring(fileName.lastIndexOf("."));
                try {
                    file.transferTo(new File(path, fileName));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @RequestMapping("/showDownload")
    public String showDownload(){
        return "download";
    }

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(HttpSession session) throws IOException {
        // 获取需要下载的服务器上的文件资源
        String path = session.getServletContext().getRealPath("/js/jquery-3.4.1.min.js");
        File file = new File(path);

        HttpHeaders headers = new HttpHeaders();
        // 参数一:指定资源的下载方式
            // 其值有两种
            // attachment:以附件的方式下载
            // inline:在线打开
        // 参数二:指定显示的资源名
        headers.setContentDispositionFormData("attachment","jquery-3.4.1.min.js");
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers,HttpStatus.CREATED);
    }
}
